# Copyright 2018- The Pixie Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

load("//bazel:pl_build_system.bzl", "pl_cc_library_internal")

package(default_visibility = ["//src:__subpackages__"])

## https://github.com/bazelbuild/bazel/issues/1992
## Borrowed from envoy build.
genrule(
    name = "gen_version_linkstamp",
    srcs = ["version_linkstamp.cc"],
    outs = ["gen_version_linkstamp.cc"],
    cmd = """
    STABLE_BUILD_SCM_REVISION=$$(
          grep STABLE_BUILD_SCM_REVISION bazel-out/stable-status.txt \\
        | sed 's/^STABLE_BUILD_SCM_REVISION //')
    STABLE_BUILD_SCM_STATUS=$$(
          grep STABLE_BUILD_SCM_STATUS bazel-out/stable-status.txt \\
        | sed 's/^STABLE_BUILD_SCM_STATUS //')
    STABLE_BUILD_TAG=$$(
          grep STABLE_BUILD_TAG bazel-out/stable-status.txt \\
        | sed 's/^STABLE_BUILD_TAG //')
    STABLE_BUILD_NUMBER=$$(
          grep STABLE_BUILD_NUMBER bazel-out/stable-status.txt \\
        | sed 's/^STABLE_BUILD_NUMBER //')
    STABLE_BUILT_BY=$$(
          grep STABLE_BUILT_BY bazel-out/stable-status.txt \\
        | sed 's/^STABLE_BUILT_BY //')


    BUILD_TIMESTAMP=$$(
          grep BUILD_TIMESTAMP bazel-out/volatile-status.txt \\
        | sed 's/^BUILD_TIMESTAMP //')

    echo "#define BUILD_SCM_REVISION \\"$$STABLE_BUILD_SCM_REVISION\\"" >> $@
    echo "#define BUILD_SCM_STATUS \\"$$STABLE_BUILD_SCM_STATUS\\"" >> $@
    echo "#define BUILD_TAG \\"$$STABLE_BUILD_TAG\\"" >> $@
    echo "#define BUILD_NUMBER \\"$$STABLE_BUILD_NUMBER\\"" >> $@
    echo "#define BUILT_BY \\"$$STABLE_BUILT_BY\\"" >> $@

    echo "#define BUILD_TIMESTAMP $$BUILD_TIMESTAMP" >> $@
    cat $(location :version_linkstamp.cc) >> $@
    """,
    stamp = True,
    visibility = ["//visibility:private"],
)

pl_cc_library_internal(
    name = "cc_library",
    srcs = [
        "version.cc",
    ],
    hdrs = ["version.h"],
)

pl_cc_library_internal(
    name = "version_linkstamp",
    srcs = select({
        "//bazel:stamped": [":gen_version_linkstamp"],
        "//conditions:default": ["placeholder_version_linkstamp.cc"],
    }),
    # Usage of this library has serious implications for caching. The use should
    # be restricted to binaries.
    # TODO(zasgar): Refactor dependent code so we can more precisely apply the visbility rules.
    visibility = [
        "//src/carnot/planner/docs:__pkg__",
        "//src/experimental:__subpackages__",
        "//src/vizier/funcs:__pkg__",
        "//src/vizier/services/agent/kelvin:__pkg__",
        "//src/vizier/services/agent/pem:__pkg__",
    ],
)

# This is a version of the library that can be linked with tests to prevent
# busting the cache.
pl_cc_library_internal(
    name = "test_version_linkstamp",
    testonly = 1,
    srcs = [
        "placeholder_version_linkstamp.cc",
    ],
)
